package com.molichuxing.services.infrastructure.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.services.infrastructure.bean.StockCarBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.StockCarMapper;
import com.molichuxing.services.infrastructure.dto.request.create.StockCarCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.StockCarModifyDto;
import com.molichuxing.services.infrastructure.dto.response.StockCarDownloadDto;
import com.molichuxing.services.infrastructure.dto.response.StockCarDto;
import com.molichuxing.services.infrastructure.service.StockCarService;
import com.molichuxing.services.property.StockCarCarStatusEnum;
import com.molichuxing.services.property.StockCarStatusEnum;
import com.molichuxing.services.property.StockCarStockTypeEnum;
/**
 * 
 * @author liyg
 * 库存车辆服务实现类
 * 2019-08-22
 *
 */
@Service("stockCarService")
public class StockCarServiceImpl implements StockCarService{

	@Autowired
	private StockCarMapper stockCarMapper;

	/**
	 * 获取库存信息
	 * 
	 * @param carId 
	 *           车辆id
	 * @return
	 */
	@Override
	public StockCarDto getStockCar(Integer carId) {
	    
		StockCarBean stockCar=stockCarMapper.getStockByCarId(carId);
		StockCarDto stockCarDto = Convert.toStockCarDto(stockCar);
		
		return stockCarDto;
	}
	
	/**
	 * 获取库存信息（根据车辆id查询车辆入店时间）
	 * 
	 * @param carId 
	 *           车辆id
	 * @return
	 */
	@Override
	public StockCarDto getStorageTime(Integer carId) {
		StockCarBean stockCar=stockCarMapper.getStorageTime(carId);
		StockCarDto stockCarDto = Convert.toStockCarDto(stockCar);
		
		return stockCarDto;
	}
	
	
	/**
	 * 获取库存车辆详情
	 * @param carIds
	 * @return
	 */
	@Override
	public List<StockCarDto> getStockCars(List<Integer> carIds) {
		List<StockCarBean> list=stockCarMapper.getStockCars(carIds);
		List<StockCarDto> stockCarDtoList = Convert.toStockCarDto(list);
		return stockCarDtoList;
	}
	
	/**
	 *库存车辆列表
	 * @param map 参数
	 *key:carIds      List      车辆ids 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:status     Integer 库存状态 非必填
	 *key:type       Integer 出入库类型 非必填
	 *key:operationState Integer   运营状态 非必填
	 *key:inStockType  Integer 收车库类型 非必填
	 *key:stockId    Integer 中转库id 非必填
	 *key:startTime  LocalDateTime 开始时间 非必填
	 *key:endTime    LocalDateTime 结束时间 非必填
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarDto> getStockCarList(Map<String, Object> map) {
		long total=stockCarMapper.getPageCount(map);
		if(total <= 0) {
			return null;
		}
		
		Integer pageNum=ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize=ObjectCastUtil.castInteger(map.get("pageSize"));
		List<StockCarBean> list=stockCarMapper.getPage(map,pageNum,pageSize);
		List<StockCarDto> stockCarDtoList = Convert.toStockCarDto(list);
	    Paged<StockCarDto> paged=new Paged<>(stockCarDtoList,total,pageSize);
		
		return paged;
	}
	
	/**
	 * 获取数量
	 * 
	 * @param status 
	 *            库存状态（1：待入库，2：已入库）
	 * @param inStockType
	 *            收车库类型（1：中转库，2：门店）  
	 * @return
	 */
	@Override
	public Long getCount(Integer status, Integer inStockType) {
		
		return stockCarMapper.getCount(status,inStockType,null,null);
	}
	/**
	 * 判断该车是否在库
	 * 
	 * @param carId 
	 *           车辆id
	 * @return true：存在   false：不存在  
	 */
	@Override
	public Boolean getStockCarExist(Integer carId) {
		
		StockCarBean stockCar=stockCarMapper.getStockByCarId(carId);
		StockCarDto stockCarDto = Convert.toStockCarDto(stockCar);
		if(null == stockCarDto) {
			return false;
		}
		
		if(null == stockCar.getInStockType()) {
			return false;
		}
		
		if(StockCarStatusEnum.YET_STOCK.getValue() == stockCar.getStatus() 
				&& StockCarStockTypeEnum.ENTREPOT_STORAGE.getValue() == stockCar.getInStockType() ) {
			return true;
		}else {
			return false;
		}
	}
	
	/**
	 * 保存库存信息
	 * 
	 * @param createDto
	 * @return
	 */
	@Override
	public Boolean create(StockCarCreateDto createDto) {
		
		StockCarBean stockCar=stockCarMapper.getStockByCarId(createDto.getCarId());
		if(null != stockCar) {
			return false;
		}
		
		StockCarBean stockCarBean=new StockCarBean();
		BeanUtils.copyProperties(createDto, stockCarBean);
		
		int num = stockCarMapper.create(stockCarBean);
		
		return num == 1 ? true : false;
	}
	
	/**
	 * 修改库存信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modify(StockCarModifyDto modifyDto) {
		
		StockCarBean stockCarBean=new StockCarBean();
		BeanUtils.copyProperties(modifyDto, stockCarBean);
		
		int num = stockCarMapper.updateById(stockCarBean);
		
		return num ==1 ? true : false;
	}
    
	/**
	 * 修改库存信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyStatus(StockCarModifyDto modifyDto) {

		StockCarBean stockCarBean=new StockCarBean();
		BeanUtils.copyProperties(modifyDto, stockCarBean);
		
		int num = stockCarMapper.updateStatusById(stockCarBean);
		
		return num ==1 ? true : false;
	}
	
	/**
	 * 修改库存信息
	 * 
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyInStockId(StockCarModifyDto modifyDto) {

		StockCarBean stockCarBean=new StockCarBean();
		BeanUtils.copyProperties(modifyDto, stockCarBean);
		
		int num = stockCarMapper.updateInStockIdById(stockCarBean);
		
		return num ==1 ? true : false;
	}
	
	/**
	 *库存车辆列表（经销商）
	 * @param map 参数
	 *key:carIds     List 车辆ids 非必填
	 *key:vehicleId  Integer 车型id 非必填
	 *key:stockId    Integer 仓库id 非必填
	 *key:stockIds   List 仓库ids
	 *key:status     Integer 库存状态（1：待入库，2：已入库）
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarDto> getDealerStockCarList(Map<String, Object> map) {
		
		map.put("status", StockCarStatusEnum.YET_STOCK.getValue());
		map.put("carStatus", StockCarCarStatusEnum.FOR_RENT.getValue());
		
		long total = stockCarMapper.getDealerListCount(map);
		if(total <= 0) {
			return null;
		}
		
		Integer pageNum=ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize=ObjectCastUtil.castInteger(map.get("pageSize"));
		List<StockCarBean> list=stockCarMapper.getDealerList(map,pageNum,pageSize);
		List<StockCarDto> stockCarDtoList = Convert.toStockCarDto(list);
	    Paged<StockCarDto> paged=new Paged<>(stockCarDtoList,total,pageSize);
		return paged;
	}
	
	/**
	 *到期还车列表（经销商）
	 * @param map 参数
	 *key:carIds      List 车辆ids 非必填
	 *key:type        Integer 入库类型（1：采购，2：调拨，3、拒收、4、收车） 非必填
	 *key:status      Integer 库存状态（1：待入库，2：已入库） 非必填
	 *key:inStockType Integer 收车库类型（1：中转库，2：门店） 非必填
	 *key:sortName   String 排序字段 
	 *key:sortWay    String 排序方式 
	 *key:pageNum    Integer 当前页
	 *key:pageSize   Integer 每页显示条数
	 * @return
	 */
	@Override
	public Paged<StockCarDto> getReturnStockCarList(Map<String, Object> map) {
		long total = stockCarMapper.getReturnStockCarListCount(map);
		if(total <= 0) {
			return null;
		}
		
		Integer pageNum=ObjectCastUtil.castInteger(map.get("pageNum"));
		Integer pageSize=ObjectCastUtil.castInteger(map.get("pageSize"));
		List<StockCarBean> list=stockCarMapper.getReturnStockCarList(map,pageNum,pageSize);
		List<StockCarDto> stockCarDtoList = Convert.toStockCarDto(list);
	    Paged<StockCarDto> paged=new Paged<>(stockCarDtoList,total,pageSize);
		return paged;
	}
	
	/**
	 * 修改车辆运营状态
	 * @param carId
	 * @param carStatus (0：待出租，1：已出租，2：不可租)
	 * @return
	 */
	@Override
	public Boolean modifyCarStatus(Integer carId, Integer carStatus) {
		
		int num = stockCarMapper.modifyCarStatus(carId,carStatus);
		return num == 1 ? true : false;
	}
	
	/**
	 * 修改车辆运营状态
	 * @param carId
	 * @param carStatus (0：待出租，1：已出租，2：不可租)
	 * @param fenceId 围栏id
	 * @return
	 */
	@Override
	public Boolean modifyCarStatusAndFenceId(Integer carId, Integer carStatus, Integer fenceId) {
		int num = stockCarMapper.modifyCarStatusAndFenceId(carId,carStatus,fenceId);
		return num == 1 ? true : false;
	}
	/**
	 * 车辆调拨(修改库存信息 包含调拨说明)
	 * @param modifyDto
	 * @return
	 */
	@Override
	public Boolean modifyAllotStock(StockCarModifyDto modifyDto) {
		
		StockCarBean stockCarBean=new StockCarBean();
		BeanUtils.copyProperties(modifyDto, stockCarBean);
		
		int num = stockCarMapper.updateAllotStock(stockCarBean);
		
		return num ==1 ? true : false;
	}

	/**
	 * 获取数量
	 * 
	 * @param status 
	 *            库存状态（1：待入库，2：已入库）
	 * @param inStockType
	 *            收车库类型（1：中转库，2：门店）  
	 * @param carIds
	 *            车辆ids
	 * @param type
	 *            入库类型（1：采购，2：调拨，3、拒收、4、收车）
	 * @return
	 */
	@Override
	public Long getCount(Integer status, Integer inStockType, List<Integer> carIds,Integer type) {
		return stockCarMapper.getCount(status,inStockType,carIds,type);
	}
	
	/**
	 * 获取库存信息
	 * 
	 * @param carId 
	 *           车辆id
	 * @param stockIds 
	 *           仓库ids
	 * @return
	 */
	@Override
	public StockCarDto getStockCar(Integer carId, List<Integer> stockIds) {
		StockCarBean stockCarBean = stockCarMapper.getStockCarByStockIds(carId,stockIds);
		StockCarDto stockCarDto = Convert.toStockCarDto(stockCarBean);
		return stockCarDto;
	}
	
	
	@Override
	public List<StockCarDownloadDto> getStockCarDownloadList(Map<String, Object> map) {
		List<StockCarBean> list=stockCarMapper.getStockCarDownloadList(map);
		if(null == list || list.isEmpty()) {
			return null;
		}
		
	    return getStockCarDownloadDtoList(list);
	}
	
	@Override
	public List<StockCarDownloadDto> getByIds(List<Integer> carIds) {
		List<StockCarBean> list=stockCarMapper.getStockCars(carIds);
		if(null == list || list.isEmpty()) {
			return null;
		}
		
		return getStockCarDownloadDtoList(list);
	}
	
	/**
	 * 数据转换
	 * @param list
	 * @return
	 */
	private List<StockCarDownloadDto> getStockCarDownloadDtoList(List<StockCarBean> list){
		List<StockCarDownloadDto> dtos = new ArrayList<>(list.size());
		for (StockCarBean bean : list) {
			StockCarDownloadDto  dto = new StockCarDownloadDto();
			BeanUtils.copyProperties(bean, dto);
            if(StockCarStatusEnum.WAIT_STOCK.getValue() == dto.getStatus()) {
				dto.setStockType(bean.getOutStockType());
				dto.setStockId(bean.getOutStockId());  
				dto.setStorageTime(bean.getOutOperationTime());
			}else {
				dto.setStockType(bean.getInStockType());
				dto.setStockId(bean.getInStockId());  
				dto.setStorageTime(bean.getStorageTime());
			}	
            dtos.add(dto);
		}
		return dtos;
	}
	
	/**
	 * 查询库存车辆列表导出总条数
	 * @param map
	 * @return
	 */
	@Override
	public Long getStockCarDownloadListCount(Map<String, Object> map) {
		return stockCarMapper.getStockCarDownloadListCount(map);
	}
	
	
	@Override
	public List<StockCarDownloadDto> getStockListByStatus() {
		List<StockCarBean> list=stockCarMapper.getStockListByStatus();
		if(null == list || list.isEmpty()) {
			return null;
		}
		
		return getStockCarDownloadDtoList(list);
	}
	
	/**
	 * 根据车辆id修改围栏id
	 */
	@Override
	public Boolean modifyFenceId(Integer carId, Integer fenceId) {
		int num = stockCarMapper.modifyFenceId(carId,fenceId);
		return num == 1;
	}
}
